package com.spring.boot.controller;

import com.spring.boot.pojo.Json;
import com.spring.boot.pojo.UserP;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 测试请求参数示例
 */
@RestController
public class RequestController {
    /**
     * 简单参数HttpServletRequest
     */
    @RequestMapping("/simpleParam")
    public String simpleParam(HttpServletRequest request) {
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        System.out.println("Hello World~");
        return "我的姓名：${name}" + name + "年龄" + age + "Hello World~";
    }

    /**
     * 简单参数 @RequestParam
     *
     * @param username
     * @param age
     * @return
     */
    @RequestMapping("/simpleParam2")
    public String simpleParam2(@RequestParam(name = "username", required = false) String username, Integer age) {
        System.out.println("Hello World~");
        return "我的姓名：${name}" + username + "年龄" + age + "Hello World~";
    }

    /**
     * 简单实体参数
     *
     * @param user
     * @return
     */
    @RequestMapping("/simplePojo")
    public String simplePojo(UserP user) {
        System.out.println(user);
        return user.getName() + user.getAge();
    }

    /**
     * 复杂实体参数
     *
     * @param user
     * @return
     */
    @RequestMapping("/complexPojo")
    public String complexPojo(UserP user) {
        System.out.println(user);
        System.out.println(user.getAddress());
        return user.getName() + user.getAge() + user.getAddress().getProvince() + user.getAddress().getCity();
    }

    /**
     * 数组集合参数
     *
     * @param hobby
     * @return
     */
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] hobby) {
        System.out.println(Arrays.toString(hobby));
        return "OK";
    }

    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> hobby) {
        System.out.println(hobby);
        return "OK";
    }

    /**
     * 日期时间参数
     *
     * @param updateTime
     * @return
     */
    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime) {
        System.out.println(updateTime);
        return "OK";
    }

    /**
     * json参数
     *
     * @param json
     * @return
     */
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody Json json) {
        System.out.println(json);
        return "OK";
    }

    /**
     * 路径参数
     * @param id
     * @return
     */
    @RequestMapping("/path/{id}/{name}")
    public String pathParam(@PathVariable Integer id,@PathVariable String name) {
        System.out.println(id);
        System.out.println(name);
        return "OK";
    }
}
